<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    /*
    * Main Tesing rentacar platform class
    * 
    */
    require_once($application_folder."/controllers/rentacar.php");
    Class Test_rentacar extends rentacar{

        var $locations = array(
            array('id' => 1, 'text' => 'Budva - Globtour Office', 'value' => '0'),
            array('id' => 2, 'text' => 'Budva - Hotel Avala', 'value' => '0'),
            array('id' => 3, 'text' => 'Budva - Hotel Slovenska Plaza', 'value' => '0'),
            array('id' => 4, 'text' => 'Budva - Hotel Mediteran', 'value' => '0'),
            array('id' => 5, 'text' => 'Budva - Hotel Iberostar', 'value' => '0'),
            array('id' => 6, 'text' => 'Petrovac - Hotel Palas', 'value' => '0'),
            array('id' => 7, 'text' => 'TIVAT - Airport +15 &#8364;', 'value' => '15'),
            array('id' => 8, 'text' => 'TIVAT - Hotel Palma +15 &#8364;', 'value' => '15'),
            array('id' => 9, 'text' => 'BAR - Hotel Princess +15 &#8364;', 'value' => '15'),
            array('id' => 10, 'text' => 'ULCINJ - Hotel Otrant +15 &#8364;', 'value' => '15'),
            array('id' => 11, 'text' => 'ULCINJ - Hotel Ada Bojana +15 &#8364;', 'value' => '15'),
            array('id' => 12, 'text' => 'PODGORICA - Airport +15 &#8364;', 'value' => '15'),
            array('id' => 13, 'text' => 'PODGORICA - Hotel Crna Gora +15 &#8364;', 'value' => '15'),
            array('id' => 14, 'text' => 'PODGORICA - Hotel Podgorica +15 &#8364;', 'value' => '15')        
        );
        var $toSetSessionFromSearchModule = Array (
            'source_info' => 'www.novisajt.com',
            'pickup_loc_id' => 1, 
            'fullfrom' => '08:00', 
            'return_loc_id' => 1, 
            'fullto' => '08:00', 
            'pickup_date' => '01.09.2012', 
            'return_date' => '02.09.2012' 
        ); 

        var $numberOfDays = 1;
        var $totalPrice = 0;

        function __construct()
        {
            parent ::__construct();
            $this->load->library('unit_test');
            $this->load->library('format');
        }

        function index ()
        {
            $this->_showNav();
        }

        /*
        * Test parts 
        * Destroy session  
        * Location from service
        */
        function home()
        {
            /*
            * Test session
            */
            $this->session->sess_destroy();
            $this->unit->run( count($this->session->all_userdata()), 6,'Session',"Match number of item");            

            $locations_from_rest = $this->format->to_array($this->rest->get('locations', NULL, 'json'));

            /*
            * Test locations from rest
            */
            $title = 'Test lokacija preuzimanja i vracanja';
            $text = 'Parsirao sam podatke sa rest servisa preko biblioteke Format od gospodina Phil-a,<br> iskopirao podatke sa rest servisa u lokalnu metodu i uportdio ih';            
            $this->unit->run( $locations_from_rest, $this->locations,$title,$text);

            echo $this->unit->report();
            $this->_showNav();

        }

        /*
        * Test parts   
        * Location from service
        * Car list from service
        * Number of booking days
        * Location prices
        */
        function available_fleet()
        {
            // * Create session
            $this->session->sess_destroy();
            $this->session->set_userdata($this->toSetSessionFromSearchModule);

            // * Location from service
            $locations = $this->_getLocations();

            $title = 'Test lokacija preuzimanja i vracanja';
            $text = 'Parsirao sam podatke sa rest servisa preko biblioteke Format od gospodina Phil-a,<br> iskopirao podatke sa rest servisa u lokalnu metodu i uportdio ih';            
            $this->unit->run( $this->format->to_array($locations), $this->locations,$title,$text);


            // * Search available fleet
            $post = array(
                'dateTimeFrom'=>$this->booking_session->get('pickup_date').' '.$this->booking_session->get('fullfrom'),
                'dateTimeTo'=>$this->booking_session->get('return_date').' '.$this->booking_session->get('fullto')
            );

            $response = $this->rest->post('available_cars', $post, 'json');
            $this->unit->run( $response, 'is_array','Car list from service',"Match array");
            $this->firephp->log("Car list from service result:");
            $this->firephp->log($response); 

            // * Calculate and set number of days
            $this->data['no_days'] = calculateDays($post['dateTimeFrom'],$post['dateTimeTo']);
            $this->booking_session->set('no_days', $this->data['no_days']);
            $this->toSessionArray('no_days',$this->data['no_days']);

            // * Calculate location ( pickup and return ) prices
            $this->booking_prices->step_2();
            $this->toSessionArray('tot_price',$this->booking_session->get('tot_price'));

            // * Run session mach test
            $this->unit->run($this->booking_session->get_all_data_test()==$this->toSetSessionFromSearchModule,1,'Sesion mach');

            // * Print report
            echo $this->unit->report();
            $this->_showNav();
        }

        function checkout($href)
        {

            // * Create session
            $this->session->sess_destroy();
            $this->session->set_userdata($this->toSetSessionFromSearchModule);
            $this->toSessionArray('no_days',$this->numberOfDays);
            $this->toSessionArray('tot_price',$this->totalPrice);
            $this->booking_session->set('no_days', $this->numberOfDays);
            $this->booking_session->set('tot_price', $this->totalPrice);

            // Check if first session data pickup_loc_id is set. Redirect if False 
            //$this->booking_session->check_session();

            $car_ID = idFromUrl($href,'-');
            $post = array( 
                'id'=>$car_ID
            );
            $response = $this->rest->post('get_car', $post, 'json');
            $this->unit->run( $response, 'is_array','Car details from service',"Match array");
            $this->firephp->log("Car details from service result:");
            $this->firephp->log($response);

            $this->data['car'] = $response[0];
            // * Set car_id and day_price session, unset extras if different car_id
            $this->booking_session->step_3($response[0]);
            $this->toSessionArray('car_id',$this->booking_session->get('car_id'));

            // * Calculate prices for booking days
            $this->booking_prices->step_3($response[0]);
            $this->toSessionArray('day_price',$this->booking_session->get('day_price'));
            $this->toSessionArray('tot_price',$this->toSetSessionFromSearchModule['day_price']*$this->numberOfDays);

            // * Get car accessories
            $post = array( 
                'id'=>$this->booking_session->get('car_id')
            );
            $accessories = $this->rest->post('accessories', $post, 'json');
            $this->unit->run( $response, 'is_array','Accessories from service',"Match array");
            $this->firephp->log("Accessories from service:");
            $this->firephp->log($accessories);

            //Todo: Test extras in booking_session and booking_prices
            
            // * Run session mach test
            $this->unit->run($this->booking_session->get_all_data_test()==$this->toSetSessionFromSearchModule,1,'Sesion mach');

            // * Print report
            echo $this->unit->report();
            $this->_showNav();
        }

        /*
        * Get locations from service and create session from Search Module
        * 
        */
        function _getLocations()
        {
            // Create session from Search Module Rentacar
            if($this->input->post('pickup_loc_id')||$this->booking_session->get('pickup_loc_id'))
            {
                $locations = $this->rest->get('locations', NULL, 'json');
                if($locations)
                {
                    $this->booking_session->step_2($locations);
                    return $locations;
                }else {echo 'Error: Location on availibale_fleet method';}

            }else {echo 'redirect';}
        }

        function _showNav()
        {
            $html = '<ul>';
            $html .= '<li><a href="'.base_url("test_rentacar/home").'">Home / Locations</a></li>';
            $html .= '<li><a href="'.base_url("test_rentacar/available_fleet").'">Available Fleet</a></li>';
            $html .= '<li><a href="'.base_url("test_rentacar/checkout/daewoo-kalos-43").'">Checkout daewoo-kalos-43</a></li>';
            $html .= '<li><a href="'.base_url("rentacar/home").'">Go to website</a></li>';
            $html .= '</ul>';
            echo $html;
        }

        function toSessionArray($key,$value)
        {
            $this->toSetSessionFromSearchModule[$key] = $value;
        }
    }
?>